package cn.hncu.booksManagementSystem.in.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.BMS_APP;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;

public class AddPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTextField tfdSumNum;
	private JFrame mainFrame;
	private List<InDetailModel> details;
	private JComboBox<BookModel> cbbxBook;
	private JLabel labInUserName;
	/**
	 * Create the panel.
	 * @param details 
	 */
	public AddPanel(JFrame mainFrame, List<InDetailModel> details) {
		this.mainFrame = mainFrame;
		this.details = details;
		initContentPane();
		initData();
	}
	private void initData() {
		//初始化图书
		Collection<BookModel> books = BookEbiFactory.getInstanceOfBookEbi().getAll();
		for (BookModel book : books) {
			cbbxBook.addItem(book);
		}
		labInUserName.setText(BMS_APP.activeUser.getName());
	}
	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("订单明细");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(317, 55, 145, 38);
		add(label);
		
		cbbxBook = new JComboBox<BookModel>();
		cbbxBook.setMaximumRowCount(4);
		cbbxBook.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cbbxBook.setBounds(209, 154, 400, 38);
		add(cbbxBook);
		
		
		JLabel lblNewLabel = new JLabel("图书：");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		lblNewLabel.setBounds(104, 155, 95, 38);
		add(lblNewLabel);
		
		JLabel label_1 = new JLabel("购买数量：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_1.setBounds(160, 243, 145, 38);
		add(label_1);
		
		tfdSumNum = new JTextField();
		tfdSumNum.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdSumNum.setBounds(316, 243, 199, 38);
		add(tfdSumNum);
		tfdSumNum.setColumns(10);
		
		JLabel label_2 = new JLabel("进货人：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_2.setBounds(160, 332, 145, 38);
		add(label_2);
		
		JButton btnAdd = new JButton("添加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnAddActionPerformed(e);
			}
		});
		btnAdd.setForeground(Color.BLUE);
		btnAdd.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnAdd.setBounds(190, 424, 113, 45);
		add(btnAdd);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new InPanel(mainFrame,details));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(457, 424, 113, 45);
		add(btnBack);
		
		labInUserName = new JLabel("");
		labInUserName.setForeground(Color.RED);
		labInUserName.setHorizontalAlignment(SwingConstants.CENTER);
		labInUserName.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		labInUserName.setBounds(317, 332, 145, 38);
		add(labInUserName);
	}
	
	protected void btnAddActionPerformed(ActionEvent e) {
		//1收集参数id(逻辑层补),inId(逻辑层补),bookId, bookName--为外键添加一个属性，提高用户体验 ,sumNum,sumMomey(逻辑层补)
		String bookId = ((BookModel)cbbxBook.getSelectedItem()).getId();
		String bookName = ((BookModel)cbbxBook.getSelectedItem()).getName();
		String strSumNum = tfdSumNum.getText().trim();
		int sumNum = 0;
		if ("".equals(strSumNum)) {
			JOptionPane.showMessageDialog(this, "请输入进货数量！");
			tfdSumNum.requestFocus();
			return ;
		}
		try {
			sumNum = Integer.parseInt(strSumNum);
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(this, "进货数量格式错误！");
			tfdSumNum.requestFocus();
			return ;
		}
		if (sumNum<0) {
			JOptionPane.showMessageDialog(mainFrame, "进货数量不能小于0");
			tfdSumNum.requestFocus();
			return;
		}
		//2组织参数
		InDetailModel inDetail = new InDetailModel();
		inDetail.setBookId(bookId);
		inDetail.setBookName(bookName);	
		inDetail.setSumNum(sumNum);
		//3调用details.add()
		boolean boo = details.add(inDetail);
		//4导向结果界面
		if (!boo) {
			JOptionPane.showMessageDialog(mainFrame, "添加失败");
		}
		mainFrame.setContentPane(new InPanel(mainFrame,details));
		mainFrame.validate();
	}	
}
